#!/usr/bin/env bash
# 通过docker-compose编排一系列环境进行一键快速部署运行，小白运维神器。
# https://gitee.com/zhengqingya/docker-compose

composeDirectory="/opt/linuxTools/docker-compose/"
composeTmp=${composeDirectory}
#检查系统环境
function check() {
  if [ ! -e "/opt/linuxTools/docker-compose/nginx/run.md" ]; then
    echo_red "检查安装所需命令 - Command required for installation"
    command -v git &>/dev/null || install_soft git

    if [[ "" == $(command -v docker-compose) ]]; then
      read -r -p "未发现docker-compose,使用该功能必须安装,是否下载安装(y/n):" input
      if [ "$input" == "y" ]; then
        install_docker_compose
      fi
    fi
    echo -e "开始拉取项目"

    git clone -b linux https://gitee.com/zangxw/docker-compose.git /tmp/dockerCompose
    mv /tmp/dockerCompose/Linux /opt/linuxTools/docker-compose
    sleep 1
    rm -rf /tmp/dockerCompose

  fi
}

#安装
function install() {
  read -r -p "请输入需要安装的应用名称,必须与上方目录中的名称一致: " input

  if [ "$input" != "" ]; then
    check

    # 设置要获取名称的目录
    composeTmp="${composeTmp}${input}/"
    cd ${composeTmp}
    if [ ! -f "${composeTmp}run.md" ]; then
      #不存在 可能还有文件夹
      echo "二级目录："
      ls ${composeTmp}
      install
    else
      #存在 直接运行命令
      local count=$(cat ${composeTmp}/run.md | grep "docker-compose -f docker" -c)

      if [ "1" == "${count}" ]; then
        local run=$(cat ${composeTmp}/run.md | grep "docker-compose -f docker")
        eval ${run}
        cat ${composeTmp}/run.md

        if [ $? -eq 0 ]; then
          echo_green "应用: ${input} 创建成功!"
        else
          echo_red "应用: ${input} 创建失败!"
        fi
      else

        cat ${composeTmp}/run.md
        echo -e "============================================================================\n"
        echo_red "请根据上方说明文件或查看 ${composeTmp}run.md 文件,手动执行创建!"
      fi
    fi
  fi
}

function docker_compose_linux() {

  check
  echo "目录："
  clear
  ls /opt/linuxTools/docker-compose

  echo_red "源项目地址: https://gitee.com/zhengqingya/docker-compose"
  echo_red "目录中所有的项目都使用的docker-compose,请确保系统中已安装docker-compose"

  install

}
